package br.ufmg.ubicomp.droidguide.common;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.ufmg.ubicomp.droidguide.eventservice.notification.DGNotificationType;
import br.ufmg.ubicomp.droidguide.eventservice.notification.NotificationBucket;

/**
 * memory record store for the time being
 * @author wpjr
 *
 */
public class DroidGuideConfigurationManager {

	private Map<String, String> data;
	private static DroidGuideConfigurationManager store;
	
	public static DroidGuideConfigurationManager getInstance()
	{
		if (store == null)
		{
			store = new DroidGuideConfigurationManager();
		}
		return store;
	}

	private DroidGuideConfigurationManager() {
		data = new HashMap<String, String>();
		//loadPropertiesFile();
	}

	private void loadPropertiesFile() {
		Properties properties = new Properties();
		Logger logger = Logger.getLogger(this.getClass().getSimpleName());
		try {
	        properties.load(new FileInputStream("droidguide.properties"));
	        logger.info("Reading propoerties file . . .");
	        for (Object k: properties.keySet())
	        {
	        	Object v = properties.get(k);
	        	data.put(k.toString(), v.toString());
	        	logger.info(k + " = " + v);
	        }
	    } catch (IOException e) {
	    	e.printStackTrace();
	    	NotificationBucket.getInstance().
	    		addNotification(DGNotificationType.ERROR, 
	    				System.currentTimeMillis(), e.getMessage(), 
	    				"DGRecordStore", Level.SEVERE);
	    }
	}
	
	public void addData(String key, String value)
	{
		Logger logger = Logger.getLogger(this.getClass().getSimpleName());
		logger.info("addData: " + key + " - " + value);
		data.put(key, value);
	}
	
	public String getData(String key)
	{
		return data.get(key);
	}
	
	public Collection<String> getKeys()
	{
		return data.keySet();
	}
		
}
